package com.dldata.devtools30.controller.zhibiao;

import com.alibaba.fastjson2.JSON;
import com.dldata.devtools30.entity.*;
import com.dldata.devtools30.repository.zhibiao.*;
import com.dldata.devtools30.utils.Result;
import com.dldata.devtools30.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "/DevtZbplanZbobjEntityController")
@Slf4j
public class DevtZbplanZbobjEntityController {

    @Autowired
    DevtZbplanZbobjEntityDao devtZbplanZbobjEntityDao;
    
    @Autowired
    DevtZbcfgInfoEntityDao devtZbcfgInfoEntityDao;
    
    @Autowired
    DevtZbobjInfoEntityDao devtZbobjInfoEntityDao;
    
    @Autowired
    DevtZbplanInfoEntityDao devtZbplanInfoEntityDao;
    
    @Autowired
    DevtZbobjFieldEntityDao devtZbobjFieldEntityDao;
    
    /**
     * 1、先根据方案找到已有指标及模型ID
     * 2、再根据模型ID查找指标对象，排除已有的模型ID
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "/getAllZbObjToAdd")
    @ResponseBody
    public Result getAllZbToAdd(@RequestParam Map<String, String> paramMap){
        String planId = paramMap.get("planId");
    
        List<HashMap> hashMapList=new ArrayList<>();
        List<DevtZbcfgInfoEntity> zbcfgInfoEntityList = devtZbcfgInfoEntityDao.getAllByPlanId(planId);
        Map<String, List<DevtZbcfgInfoEntity>> modelIdMap = zbcfgInfoEntityList.stream().collect(Collectors.groupingBy(DevtZbcfgInfoEntity::getModelid));
        
        //取所有模型的指标对象的交集
        List<String> zbobjIdList=null;
        Set<String> modelidList = modelIdMap.keySet();
        for (Iterator<String> iterator = modelidList.iterator(); iterator.hasNext(); ) {
            String modelid =  iterator.next();
            List<String> allZbobjidByModelidId = devtZbobjFieldEntityDao.getAllZbobjidByModelidId(modelid);
            if(zbobjIdList==null){
                zbobjIdList=allZbobjidByModelidId;
            }else{
                zbobjIdList.retainAll(allZbobjidByModelidId);
            }
        }
        List<DevtZbobjInfoEntity> allByModelId = devtZbobjInfoEntityDao.getAllByIdListAndNoPlanid(zbobjIdList,planId);
        for (int i = 0; i < allByModelId.size(); i++) {
            DevtZbobjInfoEntity devtZbobjInfoEntity = allByModelId.get(i);
            HashMap hashMap = new HashMap<>();
            hashMap.put("xh",i+1);
            hashMap.putAll(JSON.parseObject(JSON.toJSONString(devtZbobjInfoEntity)));
    
            
            hashMapList.add(hashMap);
        }
        return Result.ok(hashMapList);
    }
    
    
    @RequestMapping(value = "/getAll")
    @ResponseBody
    public Result getAll(@RequestParam Map<String, String> paramMap){
        List<HashMap> hashMapList=new ArrayList<>();
    
        Map<String, DevtZbobjInfoEntity> zbobjInfoEntityMap = devtZbobjInfoEntityDao.getAll().stream().collect(Collectors.toMap(DevtZbobjInfoEntity::getId, v -> v));
    
        List<DevtZbplanZbobjEntity> allByPlanId = devtZbplanZbobjEntityDao.getAllByPlanId(paramMap.get("planId"));
        for (int i = 0; i < allByPlanId.size(); i++) {
            DevtZbplanZbobjEntity devtZbplanZbobjEntity = allByPlanId.get(i);
            HashMap hashMap = new HashMap<>();
            hashMap.putAll(JSON.parseObject(JSON.toJSONString(devtZbplanZbobjEntity)));
            hashMapList.add(hashMap);
            
            if(zbobjInfoEntityMap.containsKey(devtZbplanZbobjEntity.getZbobjid())){
                DevtZbobjInfoEntity devtZbobjInfoEntity = zbobjInfoEntityMap.get(devtZbplanZbobjEntity.getZbobjid());
                hashMap.put("zbobjcode",devtZbobjInfoEntity.getZbobjcode());
                hashMap.put("zbobjname",devtZbobjInfoEntity.getZbobjname());
            }
        }
        return Result.ok(hashMapList);
    }
    
    
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(@RequestParam Map<String, String> paramMap){
        
        String id =paramMap.get("id");
        if(StringUtil.isEmptyOrLength0(id)){
            return  Result.error("参数有误");
        }
        try {
            DevtZbplanZbobjEntity devtZbplanZbobjEntity = devtZbplanZbobjEntityDao.findById(id).orElse(null);
            if(devtZbplanZbobjEntity!=null){
                devtZbplanZbobjEntityDao.delete(devtZbplanZbobjEntity);
            }
        }
        catch (Exception ex){
            log.error("【执行"+this.getClass().getName()+".delete() 方法失败 】异常："+ex.getMessage(),ex);
            return  Result.error("操作错误，请稍候再试!");
        }
        return  Result.ok("删除成功");
    }
    
    @RequestMapping(value = "/saveSelectZbObj")
    @ResponseBody
    public Result saveSelectZbObj(@RequestParam Map<String, String> paramMap) {
        String planid = paramMap.get("planid");
        String zbobjidStr = paramMap.get("zbobjidStr");
        String[] zbObjIdAry = zbobjidStr.split(",");
        DevtZbplanInfoEntity zbplanInfoEntity = devtZbplanInfoEntityDao.findById(planid).orElse(new DevtZbplanInfoEntity());
    
        List<DevtZbplanZbobjEntity> allByPlanId = devtZbplanZbobjEntityDao.getAllByPlanId(paramMap.get("planid"));
        for (int i = 0; i < allByPlanId.size(); i++) {
            DevtZbplanZbobjEntity devtZbplanZbobjEntity = allByPlanId.get(i);
            devtZbplanZbobjEntity.setXh(i+1);
        }
        
        for (int i = 0; i < zbObjIdAry.length; i++) {
            String zbObjId = zbObjIdAry[i];
            DevtZbplanZbobjEntity devtZbplanZbobjEntity =new DevtZbplanZbobjEntity();
            devtZbplanZbobjEntity.setId(UUID.randomUUID().toString());
    
            DevtZbobjInfoEntity devtZbcfgInfoEntity = devtZbobjInfoEntityDao.findById(zbObjId).orElse(new DevtZbobjInfoEntity());
    
            devtZbplanZbobjEntity.setZbobjid(devtZbcfgInfoEntity.getId());
            devtZbplanZbobjEntity.setZbobjcode(devtZbcfgInfoEntity.getZbobjcode());
            devtZbplanZbobjEntity.setZbobjname(devtZbcfgInfoEntity.getZbobjname());
    
            devtZbplanZbobjEntity.setPlancode(zbplanInfoEntity.getPlancode());
            devtZbplanZbobjEntity.setPlanid(zbplanInfoEntity.getId());
    
            devtZbplanZbobjEntity.setXh(allByPlanId.size()+i+1);
            devtZbplanZbobjEntityDao.save(devtZbplanZbobjEntity);
        }
    
        return Result.ok("保存成功");
    }
    
    @RequestMapping("toSelectZbObj")
    public String toSelectZbObj(String planid, Model model){
        model.addAttribute("planid",planid);
        return "thymeleaf/devtools30/zhibiao/v_zhibiaoplanSelectZbObjList";
    }
}
